package com.cweb.admin.model.bbs.bbs_category_info.service;

import com.cweb.admin.model.bbs.bbs_category_info.entity.BbsCategoryInfo;
import com.cweb.admin.model.bbs.bbs_category_info.mapper.BbsCategoryInfoMapper;
import com.cweb.framework.base.serivce.BaseMybatisService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 论坛-分类信息 服务实现类
 *
 * @since 2020-09-03
 */
@Service
public class BbsCategoryInfoService extends BaseMybatisService<BbsCategoryInfoMapper, BbsCategoryInfo> {
    /**
     * 取得文章列表
     */
    public Object getList(BbsCategoryInfo param) {
        // 取得所有分类
        List<BbsCategoryInfo> list = this.list();
        // 顶部分类
        List<BbsCategoryInfo> listCategory = new ArrayList<>();
        for (BbsCategoryInfo categoryInfo : list) {
            if (null != categoryInfo.getParent()) continue;
            // 子分类
            List<BbsCategoryInfo> listSubCategory = new ArrayList<>();
            for (BbsCategoryInfo subCategoryInfo : list) {
                if (null != subCategoryInfo.getParent() && categoryInfo.getId() == subCategoryInfo.getParent()) {
                    listSubCategory.add(subCategoryInfo);
                }
            }
            categoryInfo.setChildren(listSubCategory);
            listCategory.add(categoryInfo);
        }
        return listCategory;
    }
}
